package com.gopay.bis.ccb.bcdl.operation;

import java.io.OutputStream;
import java.io.Writer;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.HierarchicalStreamDriver;

public class XmlDeclarationXStream extends XStream {

	private String version;

	private String ecoding;
	
	private String standalone;

	public XmlDeclarationXStream() {
		this("1.0", "GB18030", "yes");
	}
	
	public XmlDeclarationXStream(String version, String ecoding, String standalone) {
		this.version = version;
		this.ecoding = ecoding;
		this.standalone = standalone;
	}
	
	public XmlDeclarationXStream(HierarchicalStreamDriver hierarchicalStreamDriver) {
		this("1.0", "GB18030", "yes", hierarchicalStreamDriver);
	}

	public XmlDeclarationXStream(String version, String ecoding, String standalone, HierarchicalStreamDriver hierarchicalStreamDriver) {
		super(hierarchicalStreamDriver);
		this.version = version;
		this.ecoding = ecoding;
		this.standalone = standalone;
	}

	public String getDeclaration() {
		return "<?xml version=\"" + this.version + "\" encoding=\""
				+ this.ecoding + "\" standalone=\""
				+ this.standalone + "\"?>"+"\n";

	}

	@Override
	public void toXML(Object arg0, OutputStream arg1) {
		try {
			String dec = this.getDeclaration();
			byte[] bytesOfDec = dec.getBytes(this.ecoding);
			arg1.write(bytesOfDec);
		} catch (Exception e) {
			return;
		}
		super.toXML(arg0, arg1);

	}

	@Override
	public void toXML(Object arg0, Writer arg1) {
		try {
			arg1.write(getDeclaration());
		} catch (Exception e) {
			return;
		}
		super.toXML(arg0, arg1);
	}
}
